package me.com.sharedprefernces2;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

public class MyAppWidget extends AppWidgetProvider {

	public static final String UPDATE_ONDEMAND = "me.com.sharedprefernces2.UPDATE_ONDEMAND";
	public static final String UPDATE_LANGUAGE = "me.com.sharedprefernces2.UPDATE_LANGUAGE";
	
	//Automatic updater
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		
		for (int i = 0; i < appWidgetIds.length; i++) {
			int appWidgetId = appWidgetIds[i];
			//Get RemoteViews
			RemoteViews views = new RemoteViews(context.getPackageName(), 
												R.layout.widget);
			//Update layout
			views.setTextViewText(R.id.tvWidgetText, "start");
			//Save changes
			appWidgetManager.updateAppWidget(appWidgetId, views);
		}
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);
		Log.d("OLO", "___________WIDGET onReceive___________");
		
		if(intent.getAction().equals(UPDATE_ONDEMAND)) {
			String myText = intent.getStringExtra("WidgetText");
			updateWidget(context, myText);
		}
		
		if(intent.getAction().equals(UPDATE_LANGUAGE)) {
			Log.d("OLO", "___________WIDGET UPDATE_LANGUAGE___________");
			updateWidgetPreferences(context);
		}
	}
	
	//On demand updater
	public void updateWidget(Context context, String myText) {
		//Get AppWidgetManager
		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
		//Get widget IDs
		ComponentName thisWidged = new ComponentName(context, MyAppWidget.class);
		int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidged);
		//Update all widget copies
		for (int i = 0; i < appWidgetIds.length; i++) {
			int appWidgetId = appWidgetIds[i];
			//Get RemoteViews
			RemoteViews views = new RemoteViews(context.getPackageName(), 
												R.layout.widget);
			//Update layout
			views.setTextViewText(R.id.tvWidgetText, myText);
			//Save changes
			appWidgetManager.updateAppWidget(appWidgetId, views);
		}
	}
	
	//On demand updater
	public void updateWidgetPreferences(Context context) {
		//Get AppWidgetManager
		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
		//Get widget IDs
		ComponentName thisWidged = new ComponentName(context, MyAppWidget.class);
		int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidged);
		//Update all widget copies
		for (int i = 0; i < appWidgetIds.length; i++) {
			int appWidgetId = appWidgetIds[i];
			//Get RemoteViews
			RemoteViews views = new RemoteViews(context.getPackageName(), 
												R.layout.widget);
			//Update layout
			//String myText = context.getString(R.string.tvWidgetText1);
			//String myText1 = application.getString(R.string.tvWidgetText1);
			views.setTextViewText(R.id.tvWidgetText1, context.getString(R.string.tvWidgetText1));
			//Save changes
			appWidgetManager.updateAppWidget(appWidgetId, views);
		}
	}
}